home *** CD-ROM | disk | FTP | other *** search
- /* LoadDriverTest.c */
- /*
- * LoadDriverTest.c
- * Copyright © 1994 Apple Computer Inc. All Rights Reserved.
- */
- /* .___________________________________________________________________________________.
- | This utility checks that a PCI 'ndrv' driver can be loaded - it opens the driver |
- | file as a shared library and checks that the required exported symbols are found. | |
- .___________________________________________________________________________________.
- */
-
- #define EXTERN /* Nothing */
- #include "LoadDriverTest.h"
-
- #if MPW
- QDGlobals qd;
- #endif
-
- void ProcessThisEvent(
- const EventRecord *eventRecordPtr,
- long *sleepTimePtr
- );
- void DoMouseEvent(
- const EventRecord *eventRecordPtr
- );
- void DoCommand(
- long menuChoice
- );
- void DoFileOpen(void);
- OSErr TestDriverFragment(
- const FSSpecPtr theFSSpecPtr
- );
- void TestCodeFragment(
- const FSSpecPtr theFSSpecPtr
- );
- Boolean CheckSymbol(
- CFragConnectionID connID,
- ConstStr255Param fileName,
- ConstStr255Param symbolName
- );
- void DoAbout(void);
- void AdjustMenus(void);
- void EnableEditMenu(void);
- pascal OSErr MyAEOpenAppHandlerFunc(
- long refCon
- );
- pascal OSErr MyAEOpenDocHandlerFunc(
- const FSSpecPtr fsSpecPtr,
- long refCon /* Event refCon */
- );
- pascal OSErr MyAEQuitAppHandlerFunc(
- long refCon
- );
- pascal void MyAEUnknownHandlerMsgFunc(
- ConstStr255Param messageText
- );
- pascal Boolean MyAEIdleProc(
- const EventRecord *theEventPtr,
- long *sleepTime,
- RgnHandle *mouseRgn
- );
- RoutineDescriptor gAEIdleUPP =
- BUILD_ROUTINE_DESCRIPTOR(uppAEIdleProcInfo, MyAEIdleProc);
- OSErr InitializeApplication(void);
-
- void
- main(void)
- {
- OSErr status;
-
- status = InitializeApplication();
- if (status == noErr) {
- InitCursor();
- while (gQuitNow == FALSE) {
- if (gUpdateMenusNeeded)
- AdjustMenus();
- WaitNextEvent(everyEvent, &gEventRecord, gSleepTime, NULL);
- gAECoreGlobals.currentEventIsAppleEvent = FALSE;
- ProcessThisEvent(&gEventRecord, &gSleepTime);
- }
- }
- ExitToShell();
- }
-
- pascal OSErr
- MyAEOpenAppHandlerFunc(
- long refCon
- )
- {
- UNUSED(refCon);
- gOpenAppCalled = TRUE;
- return (noErr);
- }
-
- pascal OSErr
- MyAEOpenDocHandlerFunc(
- const FSSpecPtr fsSpecPtr,
- long refCon /* Event refCon */
- )
- {
- UNUSED(refCon);
- TestDriverFragment(fsSpecPtr);
- /*
- * If we are launched by dropping one or more ndrv files on the icon,
- * quit after processing the last file.
- */
- if (gOpenAppCalled == FALSE)
- gQuitNow = TRUE;
- return (noErr);
- }
-
- pascal OSErr
- MyAEQuitAppHandlerFunc(
- long refCon
- )
- {
- UNUSED(refCon);
- Message("\pMyAEQuitAppHandlerFunc");
- gQuitNow = TRUE;
- return (noErr);
- }
-
-
- pascal void
- MyAEUnknownHandlerMsgFunc(
- ConstStr255Param messageText
- )
- {
- NonFatalError(errAEHandlerNotFound, messageText);
- }
-
-
- void
- ProcessThisEvent(
- const EventRecord *eventRecordPtr,
- long *sleepTimePtr
- )
- {
- long menuChoice;
- register WindowPtr theWindow;
- GrafPtr savePort;
- Boolean isActivating;
- OSErr status;
-
- theWindow = FrontWindow();
- switch (eventRecordPtr->what) {
- case nullEvent:
- break;
- case keyDown:
- case autoKey:
- if ((eventRecordPtr->message & charCodeMask) == '.'
- && (eventRecordPtr->modifiers & cmdKey) != 0) {
- FlushEvents(keyDown | autoKey, 0);
- gQuitNow = TRUE;
- }
- else if ((eventRecordPtr->modifiers & cmdKey) != 0) {
- if (eventRecordPtr->what == keyDown) {
- menuChoice = MenuKey(eventRecordPtr->message & charCodeMask);
- if (HiWord(menuChoice) != 0)
- DoCommand(menuChoice);
- }
- }
- break;
- case mouseDown:
- DoMouseEvent(eventRecordPtr);
- break;
- case updateEvt:
- theWindow = (WindowPtr) eventRecordPtr->message;
- GetPort(&savePort);
- SetPort(theWindow);
- BeginUpdate(theWindow);
- EraseRect(&theWindow->portRect);
- DrawControls(theWindow);
- DrawGrowIcon(theWindow);
- EndUpdate(theWindow);
- SetPort(savePort);
- break;
- case activateEvt:
- theWindow = (WindowPtr) eventRecordPtr->message;
- isActivating = ((eventRecordPtr->modifiers & activeFlag) != 0);
- goto activateEvent;
- break;
- case kHighLevelEvent:
- status = AEProcessAppleEvent(eventRecordPtr);
- if (status != noErr)
- NonFatalError(status, "\pAppleEvent error");
- break;
- case osEvt:
- switch (((unsigned long) eventRecordPtr->message) >> 24) {
- case mouseMovedMessage:
- break;
- case suspendResumeMessage:
- isActivating = ((eventRecordPtr->message & 0x01) != 0);
- activateEvent: if (isActivating) {
- /*
- * Activate this window. Activate events define theWindow
- * from the event record, while suspend/resume uses the
- * pre-set FrontWindow value.
- */
- SelectWindow(theWindow);
- (void) TEFromScrap();
- }
- gInForeground = isActivating;
- *sleepTimePtr = (gInForeground) ? 6 : 60;
- gUpdateMenusNeeded = TRUE;
- break;
- }
- break;
- }
- }
-
- /*
- * DoMouseEvent
- * The user clicked on something. Handle application-wide processing here, or call
- * a Catalog Browser function for specific action.
- */
- void
- DoMouseEvent(
- const EventRecord *eventRecordPtr
- )
- {
- WindowPtr theWindow;
- short whichPart;
-
- whichPart = FindWindow(eventRecordPtr->where, &theWindow);
- switch (whichPart) {
- case inMenuBar:
- InitCursor();
- DoCommand(MenuSelect(eventRecordPtr->where));
- break;
- }
- }
-
- void
- DoCommand(
- long menuChoice
- )
- {
- short menuItem;
- Str255 menuText;
- GrafPtr savePort;
-
- menuItem = LoWord(menuChoice);
- switch (HiWord(menuChoice)) {
- case MENU_Apple:
- if (menuItem == kAppleAbout)
- DoAbout();
- else {
- EnableEditMenu();
- GetMenuItemText(gAppleMenu, menuItem, menuText);
- GetPort(&savePort);
- OpenDeskAcc(menuText);
- SetPort(savePort);
- gUpdateMenusNeeded = TRUE;
- }
- break;
- case MENU_File:
- switch (menuItem) {
- case kFileOpen:
- DoFileOpen();
- break;
- case kFileQuit:
- gQuitNow = TRUE;
- break;
- }
- break;
- }
- HiliteMenu(0);
- }
-
- /*
- * AdjustMenus
- * Enable/disable menu options.
- */
- void
- AdjustMenus(void)
- {
- EnableItem(gAppleMenu, kAppleAbout);
- EnableItem(gFileMenu, kFileQuit);
- EnableItem(gFileMenu, kFileOpen);
- DisableItem(gEditMenu, kEditUndo);
- DisableItem(gEditMenu, kEditCut);
- DisableItem(gEditMenu, kEditCopy);
- DisableItem(gEditMenu, kEditPaste);
- DisableItem(gEditMenu, kEditClear);
- gUpdateMenusNeeded = FALSE;
- }
-
- /*
- * EnableEditMenu
- * Enable Edit Menu options.
- */
- void
- EnableEditMenu(void)
- {
- EnableItem(gEditMenu, kEditUndo);
- EnableItem(gEditMenu, kEditCut);
- EnableItem(gEditMenu, kEditCopy);
- EnableItem(gEditMenu, kEditPaste);
- EnableItem(gEditMenu, kEditClear);
- }
-
-
- void
- DoFileOpen(void)
- {
- Point where;
- SFTypeList typeList;
- SFReply sfReply;
- FSSpec theFSSpec;
- OSErr status;
-
- SetPt(&where, 0, 0);
- typeList[0] = 'ndrv';
- SFGetFile(
- where,
- NULL,
- NULL,
- 1,
- typeList,
- NULL,
- &sfReply
- );
- if (sfReply.good == FALSE)
- status = abortErr;
- else {
- status = FSMakeFSSpec(
- sfReply.vRefNum,
- 0,
- sfReply.fName,
- &theFSSpec
- );
- CheckStatus(status, "\pFSMakeFSSpec failed");
- }
- if (status == noErr) {
- status = TestDriverFragment(&theFSSpec);
- if (status != noErr)
- TestCodeFragment(&theFSSpec);
- }
- }
-
-
- /*
- * Test whether we can load a driver.
- */
- OSErr
- TestDriverFragment(
- const FSSpecPtr theFSSpecPtr
- )
- {
- OSErr status;
- CFragConnectionID connID;
- DriverEntryPointPtr fragmentMain;
- DriverDescriptionPtr theDriverDesc;
- Str15 errValue;
-
- status = GetDriverDiskFragment(
- theFSSpecPtr,
- &connID,
- &fragmentMain,
- &theDriverDesc
- );
- if (status == noErr) {
- (void) CloseConnection(&connID);
- ParamText(
- theFSSpecPtr->name,
- theDriverDesc->driverType.nameInfoStr,
- theDriverDesc->driverOSRuntimeInfo.driverName,
- "\p"
- );
- NoteAlert(ALRT_DriverOK, NULL);
- }
- else {
- NumToString(status, errValue);
- ParamText(
- errValue,
- theFSSpecPtr->name,
- "\p(Unknown)",
- "\p"
- );
- InitCursor();
- StopAlert(ALRT_FragLoadErr, NULL);
- }
- return (status);
- }
-
- /*
- * Test whether we can load a Code Fragment -- the driver test failed;
- * this test might say why. Note: it is only correct for 'ndrv' drivers.
- */
- void
- TestCodeFragment(
- const FSSpecPtr theFSSpecPtr
- )
- {
- OSErr status;
- CFragConnectionID connID;
- Ptr mainAddress;
- Str255 errName;
- Str15 errValue;
-
- status = GetDiskFragment(
- theFSSpecPtr,
- 0, /* Offset */
- 0, // kWholeFork, /* Fragment size */
- theFSSpecPtr->name, /* Fragment name */
- kLoadCFrag, /* Load the fragment */
- &connID, /* Connection ID */
- &mainAddress, /* -> main address */
- errName
- );
- if (status != noErr) {
- NumToString(status, errValue);
- ParamText(
- errValue,
- theFSSpecPtr->name,
- errName,
- "\p"
- );
- InitCursor();
- StopAlert(ALRT_FragLoadErr, NULL);
- }
- else {
- /*
- * The fragment is present, make sure the exported symbols
- * are also present. Note the single & -- this ensures that
- * CheckSymbol is called for both exported symbols.
- */
- if (CheckSymbol(connID, theFSSpecPtr->name, "\pTheDriverDescription")
- & CheckSymbol(connID, theFSSpecPtr->name, "\pDoDriverIO")) {
- ParamText(theFSSpecPtr->name, "\p(?)", "\p(?)", "\p");
- NoteAlert(ALRT_DriverOK, NULL);
- }
- (void) CloseConnection(&connID);
- }
- }
-
- Boolean
- CheckSymbol(
- CFragConnectionID connID,
- ConstStr255Param fileName,
- ConstStr255Param symbolName
- )
- {
- OSErr status;
- Str15 errValue;
- Ptr symAddr;
- CFragSymbolClass symClass;
-
- status = FindSymbol(
- connID,
- symbolName,
- &symAddr,
- &symClass
- );
- if (status != noErr) {
- NumToString(status, errValue);
- ParamText(
- errValue,
- fileName,
- symbolName,
- "\p"
- );
- InitCursor();
- StopAlert(ALRT_SymbolErr, NULL);
- }
- return (status == noErr);
- }
-
- void
- DoAbout(void)
- {
- GrafPtr savePort;
- DialogPtr dialog;
- short dialogItem;
-
- dialog = GetNewDialog(DLOG_About, NULL, (WindowPtr) -1L);
- if (dialog != NULL) {
- GetPort(&savePort);
- SetPort(dialog);
- ShowWindow(dialog);
- ModalDialog(NULL, &dialogItem);
- DisposeDialog(dialog);
- SetPort(savePort);
- }
- }
-
- void
- NonFatalError(
- OSErr errorStatus,
- ConstStr255Param errorMsg
- )
- {
- if (ErrorMessage(ALRT_NonFatalError, errorStatus, errorMsg) == kOKButton)
- gQuitNow = TRUE;
- }
-
- void
- FatalError(
- OSErr errorStatus,
- ConstStr255Param errorMsg
- )
- {
- ErrorMessage(ALRT_FatalError, errorStatus, errorMsg);
- ExitToShell();
- }
-
- void
- Message(
- ConstStr255Param message
- )
- {
- ParamText(message, "\p", "\p", "\p");
- NoteAlert(ALRT_Empty, NULL);
- }
-
- short
- ErrorMessage(
- short alertID,
- OSErr errorStatus,
- ConstStr255Param errorMsg
- )
- {
- Handle errorTextHdl;
- StringPtr errorTextPtr;
- Str15 errorStatusText;
- short result;
-
- if (AEInteractionOK((AEIdleUPP) &gAEIdleUPP) == FALSE)
- result = kCancelButton;
- else {
- NumToString(errorStatus, errorStatusText);
- errorTextHdl = GetResource('Estr', errorStatus);
- if (errorTextHdl != NULL) {
- HLock(errorTextHdl);
- errorTextPtr = (StringPtr) errorTextHdl;
- }
- else {
- errorTextPtr = "\pSystem Error";
- }
- ParamText(errorStatusText, errorTextPtr, errorMsg, "\p");
- InitCursor();
- result = StopAlert(alertID, NULL);
- if (errorTextHdl != NULL)
- ReleaseResource(errorTextHdl);
- }
- return (result);
- }
-
- pascal Boolean
- MyAEIdleProc(
- const EventRecord *theEventPtr,
- long *sleepTime,
- RgnHandle *mouseRgn
- )
- {
- UNUSED(mouseRgn);
- if (theEventPtr->what == kHighLevelEvent)
- return (TRUE);
- else {
- ProcessThisEvent(theEventPtr, sleepTime); /* Application specific */
- return (FALSE);
- }
- }
-
- OSErr
- InitializeApplication(void)
- {
- OSErr status;
- short i;
-
- status = noErr;
- MaxApplZone();
- InitGraf(&qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(0);
- for (i = 0; i < 8; i++)
- MoreMasters();
- for (i = 0; i < 3; i++)
- EventAvail(everyEvent, &gEventRecord);
- HNoPurge((Handle) GetCursor(watchCursor));
- SetCursor(*GetCursor(watchCursor));
- SetMenuBar(GetNewMBar(MBAR_MenuBar));
- gAppleMenu = GetMenuHandle(MENU_Apple);
- if (gAppleMenu == NULL) {
- status = resNotFound;
- FatalError(resNotFound, "\pNo Apple Menu");
- }
- if (status == noErr) {
- AppendResMenu(gAppleMenu, 'DRVR');
- gFileMenu = GetMenuHandle(MENU_File);
- if (gFileMenu == NULL) {
- status = resNotFound;
- FatalError(resNotFound, "\pNo File Menu");
- }
- }
- if (status == noErr) {
- DrawMenuBar();
- gUpdateMenusNeeded = TRUE;
- status = InitializeAppleEvents(
- MyAEOpenAppHandlerFunc, /* Open application */
- MyAEOpenDocHandlerFunc, /* Open document */
- NULL, /* No Print Doc handler */
- MyAEQuitAppHandlerFunc, /* Quit application */
- NULL, /* No Do Script Callback */
- MyAEUnknownHandlerMsgFunc, /* Strange AppleEvent */
- 0 /* RefCon */
- );
- if (status != noErr)
- FatalError(status, "\pCan't initialize AppleEvents");
- }
- return (status);
- }